<?php
/**
 * Smarty plugin
 * @package com.clear-health.celini
 * @subpackage smarty_plugins
 */


/**
 * Insure required files are loaded.
 */
require_once CELINI_ROOT . '/includes/DateObject.class.php';
require_once CELINI_ROOT . '/includes/TimestampObject.class.php';


/**
 * Smarty {clni_input_date} function plugin
 *
 * Type:     function<br>
 * Name:     clni_input_date<br>
 * Input:<br>
 *           - name       (required)  
 *           - value      (required)
 *           - size       (optional)    default = 8
 *           - format     (optional)    php date function format
 *           - format_popup     (optional)    % style date format
 *           - id         (optional)    autogenerated if not set
 *           - required   (optional)    default = false
 * Purpose:  Prints an input field for dates with an extra popup selection widget
 *
 *
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_clni_input_date($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
    static $id_counter = 1;
    
    $name = null;
    $value = null;
    $id = false;
    $size = 10;
    $format = "m/d/Y";
    $format_popup = DateObject::getFormat();
    $required = false;
    $time = false;
    $extra = "";

    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'name':
                $$_key = (string)$_val;
                break;

            case 'value':
                $$_key = (string)$_val;
                break;
            case 'disabled':
            case 'format':
                $$_key = (string)$_val;
                break;

            case 'size':
                $$_key = (int)$_val;
                break;

            case 'id':
                $$_key = (string)$_val;
                break;

            case 'format_popup':
                $$_key = (string)$_val;
                break;

            case 'extra':
                $$_key = (string)$_val;
                break;

            case 'required':
                    if ($_val === "true") {
                            $_val = true;
                    }
                    if ($_val === "false") {
                            $_val = false;
                    }
                $$_key = (bool)$_val;
                break;

			case 'time':
                    if ($_val == "true" || $_val == true) {
                        $_val = true;
                        $format = "Y-m-d H:i:s";
                    }
                    if ($_val === "false") {
                            $_val = false;
                    }
                $$_key = (bool)$_val;
			
            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_options: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

    if (!isset($name) && !isset($value))
        return ''; /* raise error here? */

    if ($value == "0000-00-00" || $value == "00/00/0000") {
            $value = "";
    }
    if (!empty($value)) {
            $value = date($format,strtotime($value));
    }

    if (!$id) {
            $id = "_cal_$id_counter";
            $id_counter++;
    }

    $base_dir = dirname($_SERVER['SCRIPT_NAME']) ."/";
    if ($base_dir == "//") {
            $base_dir = "/";
    }


    $req = "clni_register_validation_rule('$id','date','alert');";
    if ($required) {
            $req .= "clni_register_validation_rule('$id','required','alert');";
    }
    if ($disabled == "disabled") $disabled = ' disabled="disabled" ';

    $_html_result = '<nobr class="jscal"><input name="' . $name . '" value="' .$value . '" size="' . $size . '" id="' . $id . '"' . $extra . $disabled .
    '><img id="' . $id . '_trigger" src="'.Celini::link('stock/set_time.gif', false, 'Images').'" style="cursor: pointer; vertical-align: top"></nobr>' . "\n";
    $_html_result .= 
    '<script type="text/javascript">Calendar.setup( {inputField:"'.$id.'", showsTime: '.($time ? 'true' : 'false').',ifFormat:"'.$format_popup.'",button:"'.$id.'_trigger" }); '.$req.'</script>';

    return $_html_result;

}
/* vim: set expandtab: */

?>
